//
//  QWCommonMethod.m
//  qwm
//
//  Created by kevin on 2023/3/26.
//

#import "QWCommonMethod.h"
#import "QWMainTabBarController.h"

@implementation QWCommonMethod
static QWCommonMethod *_sharedInstance = nil;
static dispatch_once_t once_token = 0;
+(instancetype)Instance {
    dispatch_once(&once_token, ^{
        if (_sharedInstance == nil) {
            _sharedInstance = [[super allocWithZone:NULL] init] ;
        }
    });
    return _sharedInstance;
}


#pragma mark -showAlert提示
-(void)ShowAlert:(NSString*)title Msg:(NSString*)msg
{
    UIAlertView* alert=[[UIAlertView alloc] initWithTitle:title
                                                  message:msg
                                                 delegate:self
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - 当前版本号
- (NSString *)currentVersion{
    NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    return version;
}

#pragma mark - 获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC {
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    
    if ([window subviews].count ==0 )return nil;
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        result = nextResponder;
    } else {
        result = window.rootViewController;
    }
    
    /*
     *  在此判断返回的视图是不是你的根视图--我的根视图是tabbar
     */
    if ([result isKindOfClass:[QWMainTabBarController class]]) {
        QWMainTabBarController *mainTabBarVC = (QWMainTabBarController *)result;
        result = [mainTabBarVC selectedViewController];
        result = [result.childViewControllers lastObject];
    }
    
    return result;
}



@end
